﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class ModifyURL : Form
    {
        #region Attributes

        public List<string> guids = new List<string>();
        public string gid = "";
        public string urls = "";

        #endregion

        public ModifyURL()
        {
            InitializeComponent();
        }

        public ModifyURL(string id,List<string> guid,string url)
        {
            InitializeComponent();
            this.gid = id;
            this.guids = guid;
            this.urls = url;
        }

        private void ModifyURL_Load(object sender, EventArgs e)
        {
            this.tbURLs.Text = urls;
            if (guids.Count > 1)
            {
                this.tbURLs.ReadOnly = true;
                this.tbURLs.Multiline = true;
                this.tbURLs.Height += 50;
                this.Height += 50;
                this.label2.Location = new Point(this.label2.Location.X, this.label2.Location.Y + 50);
                this.cbbGroup.Location = new Point(this.cbbGroup.Location.X, this.cbbGroup.Location.Y + 50);
                this.btnOK.Location = new Point(this.btnOK.Location.X, this.btnOK.Location.Y + 50);
            }

            InitComboBox();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cbbGroup.SelectedValue == null)
            {
                MessageBox.Show("请选择分组");
                return;
            }

            int rval = guids.Count == 1 ? QT.SEO.URL.update(guids[0], this.cbbGroup.SelectedValue.ToString(), this.tbURLs.Text) : QT.SEO.URL.update(guids, this.cbbGroup.SelectedValue.ToString());

            switch (rval)
            {
                case -2:
                    MessageBox.Show("网址已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                default:
                    MessageBox.Show("修改成功");
                    this.Close();
                    this.DialogResult = DialogResult.OK;
                    break;
            }
            
        }

        public void InitComboBox()
        {
            DataTable dt = QT.SEO.Group.GetAll();
            this.cbbGroup.ValueMember = dt.Columns[0].ToString();
            this.cbbGroup.DisplayMember = dt.Columns[1].ToString();
            this.cbbGroup.DataSource = dt;
            if (string.IsNullOrEmpty(gid)) this.cbbGroup.SelectedIndex = 0;
            else this.cbbGroup.SelectedValue = gid;
        }
    }
}
